﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class mobile_authorize : System.Web.UI.Page
{
    public string openid = "";
    public string enterid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        openid = this.Request.Params["openid"].ToString();
        enterid = this.Request.Params["enterid"].ToString();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("mobile_register.aspx?openid="+openid, true);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" || TextBox2.Text == "" || TextBox1.Text == null || TextBox2.Text == null)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请填写所有必需信息！')</script>");
            return;
        }
        string input_name = TextBox1.Text;
        string input_password = TextBox2.Text;
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand com_check = new SqlCommand();
        com_check.Connection = sc;
        com_check.CommandText = "select IND_PASSWORD from INFO_IND where IND_NAME=@name";
        com_check.Parameters.AddWithValue("@name",input_name);
        object c = com_check.ExecuteScalar();
        if (c == null)//用户不存在
        {
            sc.Close();
            Page.RegisterStartupScript("ss", "<script>alert('输入的用户不存在！')</script>");
        }
        else
        {
            if (input_password.Equals(c.ToString()) == true)//密码一致，授权继续
            {
                //写入IND_CARE表

                SqlCommand get = new SqlCommand();
                get.Connection = sc;
                get.CommandText = "select IND_ID from INFO_IND where IND_NAME=@name";
                get.Parameters.AddWithValue("@name", input_name);
                string ind_id = get.ExecuteScalar().ToString();
                string dt = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
                SqlCommand com_cc = new SqlCommand();
                com_cc.Connection = sc;
                com_cc.CommandText = "select IND_ID from IND_CARE where IND_ID=@id and ENTER_ID=@idd";
                com_cc.Parameters.AddWithValue("@id", ind_id);
                com_cc.Parameters.AddWithValue("@idd", enterid);
                
                if (com_cc.ExecuteScalar() == null)//授权存在与否检测
                {
                    SqlCommand com_auth = new SqlCommand();
                    com_auth.Connection = sc;
                    com_auth.CommandText = "insert into IND_CARE values(@indid,@enter,@open,@date,null,0)";
                    com_auth.Parameters.AddWithValue("@indid", ind_id);
                    com_auth.Parameters.AddWithValue("@enter", enterid);
                    com_auth.Parameters.AddWithValue("@open", openid);
                    com_auth.Parameters.AddWithValue("@date", dt);
                     while (Application["sqllock"] == "lock")
                        {//检测是否已经锁住
                            System.Threading.Thread.Sleep(2000);
                        }
                        Application["sqllock"] = "lock";//上锁        
                    if (com_auth.ExecuteNonQuery() == 1)
                    {
                        Application["sqllock"] = "unlock";//解锁
                        //授权成功，检测该openid对应这个enterid是否在临时访客表中存在，若不存在跳过，若存在删除
                        SqlCommand com_checkvis = new SqlCommand("select VISITER_ID from INFO_VISITER where VISITER_ID=@id and ENTER_ID=@idd", sc);
                        com_checkvis.Parameters.AddWithValue("@id", openid);
                        com_checkvis.Parameters.AddWithValue("@idd", enterid);
                        object a = com_checkvis.ExecuteScalar();
                        if (a != null)
                        {
                            SqlCommand del_vis = new SqlCommand("delete from INFO_VISITER where VISITER_ID=@id and ENTER_ID=@idd", sc);
                            del_vis.Parameters.AddWithValue("@id", openid);
                            del_vis.Parameters.AddWithValue("@idd", enterid);
                             while (Application["sqllock"] == "lock")
                        {//检测是否已经锁住
                            System.Threading.Thread.Sleep(2000);
                        }
                        Application["sqllock"] = "lock";//上锁
                        try
                        {
                            del_vis.ExecuteNonQuery();
                        }
                        finally
                        {
                            Application["sqllock"] = "unlock";//解锁
                        }
                        }
                        SqlCommand com_adddeg = new SqlCommand();
                        com_adddeg.Connection = sc;
                        com_adddeg.CommandText = "update INFO_ENTER set ENTER_DEGREE=ENTER_DEGREE+10";
                        while (Application["sqllock"] == "lock")
                        {//检测是否已经锁住
                            System.Threading.Thread.Sleep(2000);
                        }
                        Application["sqllock"] = "lock";//上锁
                        try
                        {
                            com_adddeg.ExecuteNonQuery();
                        }
                        finally
                        {
                            Application["sqllock"] = "unlock";//解锁
                        }
                        //每成功增加一个授权平台增加10点热门度
                        sc.Close();
                        Page.RegisterStartupScript("ss", "<script>alert('授权成功！')</script>");

                    }
                    else
                    {
                        Application["sqllock"] = "unlock";//解锁
                        sc.Close();
                        Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重新授权！')</script>");
                    }
                }
                else
                {
                    sc.Close();
                    Page.RegisterStartupScript("ss", "<script>alert('该帐号已授权！')</script>");
                }

            }
            else//密码错误
            {
                sc.Close();
                Page.RegisterStartupScript("ss", "<script>alert('密码错误！')</script>");
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection sc2 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc2.Open();
        //判断若该openid和enterid已授权
        SqlCommand check_auth = new SqlCommand("select IND_ID from IND_CARE where IND_OPENID=@di and ENTER_ID=@ddi",sc2);
        check_auth.Parameters.AddWithValue("@di",openid);
        check_auth.Parameters.AddWithValue("@ddi",enterid);
        object a = check_auth.ExecuteScalar();
        if (a == null)
        {
            SqlCommand com_getname = new SqlCommand("select ENTER_NAME from INFO_ENTER where ENTER_ID=@id", sc2);
            com_getname.Parameters.AddWithValue("@id", enterid);
            string name = com_getname.ExecuteScalar().ToString();
            SqlCommand comm = new SqlCommand("insert into INFO_VISITER values (@openid,@enterid)", sc2);
            comm.Parameters.AddWithValue("@openid", openid);
            comm.Parameters.AddWithValue("@enterid", enterid);
            while (Application["sqllock"] == "lock")
            {//检测是否已经锁住
                System.Threading.Thread.Sleep(2000);
            }
            Application["sqllock"] = "lock";//上锁

            if (comm.ExecuteNonQuery() == 1)
            {
                Application["sqllock"] = "unlock";//解锁
                sc2.Close();
                Page.RegisterStartupScript("ss", "<script>alert('您已成为" + name + "平台的访客！')</script>");
                return;
            }
            else
            {
                Application["sqllock"] = "unlock";//解锁
                Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重试！')</script>");
                return;
            }
        }
        else
        {
            SqlCommand com_getname = new SqlCommand("select ENTER_NAME from INFO_ENTER where ENTER_ID=@id", sc2);
            com_getname.Parameters.AddWithValue("@id", enterid);
            string name = com_getname.ExecuteScalar().ToString();
            sc2.Close();
            Page.RegisterStartupScript("ss", "<script>alert('授权已存在！')</script>");
        }

    }
}